package main

func countGoodRectangles(rectangles [][]int) int {
	maxLen := 0
	lenS := make([]int, 0)
	for _, rec := range rectangles {
		lenS = append(lenS, min(rec))
		if maxLen < lenS[len(lenS)-1] {
			maxLen = lenS[len(lenS)-1]
		}
	}
	cnt := 0
	for _, num := range lenS {
		if num >= maxLen {
			cnt++
		}
	}
	return cnt
}

func min(rec []int) int {
	if rec[0] > rec[1] {
		return rec[1]
	}
	return rec[0]
}

// 官方题解，一次遍历
func countGoodRectangles(rectangles [][]int) (ans int) {
	maxLen := 0
	for _, rect := range rectangles {
		k := min(rect[0], rect[1])
		if k == maxLen {
			ans++
		} else if k > maxLen {
			maxLen, ans = k, 1
		}
	}
	return
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
